﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.RulesManager.CApRulesSetManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RenameTool.RulesManager;

[Serializable]
public class CApRulesSetManager
{
  private ObservableCollection<CApRulesSet> ruleSets = new ObservableCollection<CApRulesSet>();
  private Type[] serializableTypes;
  private readonly string mainFileName = "RenameToolRules.xml";
  private Dictionary<Guid, CApRulesSet> _UniqueIdToRuleSet = new Dictionary<Guid, CApRulesSet>();
  private static XmlSerializer xmlSerializer;
  private static CApRulesSetManager instance;

  public ObservableCollection<CApRulesSet> RuleSets
  {
    get => this.ruleSets;
    set => this.ruleSets = value;
  }

  public XmlSerializer MySerializer
  {
    get
    {
      if (CApRulesSetManager.xmlSerializer == null)
        CApRulesSetManager.xmlSerializer = new XmlSerializer(typeof (ObservableCollection<CApRulesSet>), this.serializableTypes);
      return CApRulesSetManager.xmlSerializer;
    }
  }

  private CApRulesSetManager()
  {
  }

  public static CApRulesSetManager Instance
  {
    get
    {
      if (CApRulesSetManager.instance == null)
        CApRulesSetManager.instance = new CApRulesSetManager();
      return CApRulesSetManager.instance;
    }
  }

  public void SetSerializableTypes(Type[] types) => this.serializableTypes = types;

  public string GetStoragePath()
  {
    return Path.Combine(TxApplication.CurrentUserDirectory, this.mainFileName);
  }

  public void RemoveSet(int index)
  {
    Guid uniqueId = this.RuleSets[index].UniqueId;
    this.RuleSets.RemoveAt(index);
    this._UniqueIdToRuleSet.Remove(uniqueId);
  }

  public void Clear()
  {
    this.RuleSets.Clear();
    this._UniqueIdToRuleSet.Clear();
  }

  public RuleMessage SerializeRenameRuleSet(
    string filePath,
    ObservableCollection<CApRulesSet> ruleSet)
  {
    StreamWriter streamWriter = (StreamWriter) null;
    try
    {
      streamWriter = new StreamWriter(filePath);
      this.MySerializer.Serialize((TextWriter) streamWriter, (object) ruleSet);
      streamWriter.Flush();
      streamWriter.Close();
    }
    catch (Exception ex)
    {
      return new RuleMessage(false, ex.Message);
    }
    finally
    {
      streamWriter?.Close();
    }
    return new RuleMessage(true);
  }

  public RuleMessage DeSerializeRenameRuleSet(
    string filePath,
    ref ObservableCollection<CApRulesSet> ruleSet)
  {
    StreamReader streamReader = (StreamReader) null;
    try
    {
      if (!File.Exists(filePath))
      {
        RuleMessage ruleMessage = new RuleMessage(false, "The  file does not exist");
      }
      streamReader = new StreamReader(filePath);
      ruleSet = this.MySerializer.Deserialize((TextReader) streamReader) as ObservableCollection<CApRulesSet>;
      streamReader.Close();
    }
    catch (Exception ex)
    {
      return new RuleMessage(false, ex.Message);
    }
    finally
    {
      streamReader?.Close();
    }
    return new RuleMessage(true);
  }

  public void SortRuleSetsByName()
  {
    this.ruleSets = new ObservableCollection<CApRulesSet>((IEnumerable<CApRulesSet>) this.ruleSets.OrderBy<CApRulesSet, string>((Func<CApRulesSet, string>) (i => i.Name)));
  }

  public RuleMessage SerializeRenameRuleSet()
  {
    string storagePath = this.GetStoragePath();
    this.SortRuleSetsByName();
    return this.SerializeRenameRuleSet(storagePath, this.RuleSets);
  }

  public RuleMessage DeSerializeRenameRuleSet()
  {
    string storagePath = this.GetStoragePath();
    this.Clear();
    RuleMessage ruleMessage = this.DeSerializeRenameRuleSet(storagePath, ref this.ruleSets);
    foreach (CApRulesSet ruleSet in (Collection<CApRulesSet>) this.ruleSets)
    {
      if (!this._UniqueIdToRuleSet.ContainsKey(ruleSet.UniqueId))
        this._UniqueIdToRuleSet.Add(ruleSet.UniqueId, ruleSet);
    }
    return ruleMessage;
  }

  public RuleMessage ImportRuleSet(string filePath)
  {
    ObservableCollection<CApRulesSet> ruleSet = new ObservableCollection<CApRulesSet>();
    this.DeSerializeRenameRuleSet(filePath, ref ruleSet);
    foreach (CApRulesSet newSet in (Collection<CApRulesSet>) ruleSet)
    {
      if (this._UniqueIdToRuleSet.Keys.Contains<Guid>(newSet.UniqueId))
        this.UpdateRuleSet(newSet.UniqueId, newSet);
      else
        this.AddRuleSet(newSet.Name, newSet.Description, newSet.UniqueId, newSet.Rules);
    }
    return new RuleMessage(true);
  }

  public RuleMessage ExportRuleSet(string filePath, ObservableCollection<CApRulesSet> rules)
  {
    this.SerializeRenameRuleSet(filePath, rules);
    return new RuleMessage(true);
  }

  public Guid AddRuleSet(string name, string description, Guid id, List<RuleBase> rules)
  {
    CApRulesSet capRulesSet = new CApRulesSet();
    capRulesSet.Name = name;
    capRulesSet.Description = description;
    capRulesSet.Rules = rules;
    capRulesSet.UniqueId = id;
    this.ruleSets.Add(capRulesSet);
    this._UniqueIdToRuleSet.Add(capRulesSet.UniqueId, capRulesSet);
    return capRulesSet.UniqueId;
  }

  public void UpdateRuleSetRules(Guid id, ObservableCollection<RuleBase> newRules)
  {
    if (!this._UniqueIdToRuleSet.Keys.Contains<Guid>(id))
      return;
    this._UniqueIdToRuleSet[id].Rules.Clear();
    foreach (RuleBase newRule in (Collection<RuleBase>) newRules)
      this._UniqueIdToRuleSet[id].Rules.Add(newRule);
  }

  public void UpdateRuleSet(Guid id, CApRulesSet newSet)
  {
    if (!this._UniqueIdToRuleSet.Keys.Contains<Guid>(id))
      return;
    this._UniqueIdToRuleSet[id].Name = newSet.Name;
    this._UniqueIdToRuleSet[id].Description = newSet.Description;
    this._UniqueIdToRuleSet[id].Rules.Clear();
    foreach (RuleBase rule in newSet.Rules)
      this._UniqueIdToRuleSet[id].Rules.Add(rule);
    this._UniqueIdToRuleSet[id].UniqueId = newSet.UniqueId;
  }

  public int GetIndexOfRuleSet(Guid id)
  {
    for (int index = 0; index < this.ruleSets.Count - 1; ++index)
    {
      if (this.ruleSets[index].UniqueId == id)
        return index;
    }
    return -1;
  }

  public CApRulesSet GetRuleSet(Guid id) => this._UniqueIdToRuleSet[id];
}
